package com.maiyuren.fishkit.ui.components.dict

import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.maiyuren.fishkit.dict.FishDict
import com.maiyuren.fishkit.dict.FishDictGroup
import com.maiyuren.fishkit.dict.FishDictMgr
import com.maiyuren.fishkit.utils.FishStorage

open class FishDictViewModel : ViewModel() {
    var currGroupId = mutableStateOf("")
    val groupData = mutableMapOf<String, DictGroupInner>()
    var lastSearchWord = ""
    var lastSearchIsReg = false

    class DictGroupInner(id: String, cfgKey: String, val vm: FishDictViewModel) {
        val dictGroup = mutableStateOf(getDictGroup(id))
        val currTabIndex = MutableLiveData<Int>(0)

        val tabDelegate = DictTabDelegate(dictGroup) {
            currTabIndex.value = it
        }

        init {
            var defaultTabIndex = FishStorage.getInt(currTabIndexCfgKey(id = id, cfgKey = cfgKey))
            if (defaultTabIndex < 0 || defaultTabIndex >= dictGroup.value!!.items.size) defaultTabIndex = 0
            currTabIndex.value = defaultTabIndex
            loadSelfAndBrotherDict(currTabIndex.value!!)

            currTabIndex.observeForever {
                loadSelfAndBrotherDict(it)
                FishStorage.setInt(currTabIndexCfgKey(id, cfgKey), it)
                search(word = vm.lastSearchWord, isReg = vm.lastSearchIsReg)
            }
        }

        private fun loadSelfAndBrotherDict(index: Int) {
            tabDelegate.tabs[index].dict.load()

            // pre
            var preIndex = index -1
            if (preIndex < 0) preIndex = tabDelegate.tabs.size - 1
            tabDelegate.tabs[preIndex].dict.load()


            // next
            var nextIndex = index + 1
            if (nextIndex >= tabDelegate.tabs.size) nextIndex = 0
            tabDelegate.tabs[nextIndex].dict.load()
        }

        private fun getDictGroup(id: String): FishDictGroup? {
            return FishDictMgr.groups.find {
                it.id == id
            }
        }

        private fun currTabIndexCfgKey(id: String, cfgKey: String): String {
            return "${FishStorage.keys.curr_dict_group_tab_index}_${cfgKey}_${id}"
        }

        fun search(word: String, isReg: Boolean = false, force: Boolean = false) {
            this.tabDelegate.search(index = currTabIndex.value!!, word = word, isReg = isReg, force = force)
        }

        fun getCurrDict (): FishDict {
            return tabDelegate.tabs[currTabIndex.value!!].dict
        }
    }

    init {
        // 读取配置
        val cfgGroupId = FishStorage.getString(FishStorage.keys.curr_dict_group_id)
        // 如果没有，就以第一个为准
        if (cfgGroupId != null) currGroupId.value = cfgGroupId
        else currGroupId.value = FishDictMgr.groups[0].id
    }

    fun search(word: String, isReg: Boolean = false, force: Boolean = true) {
        val group = this.groupData[this.currGroupId.value]
        val tempWord = word.trim()
        if (tempWord.isNullOrBlank()) return
        lastSearchWord = tempWord
        lastSearchIsReg = isReg
        group?.search(word = tempWord, force = force)
    }

    fun getCurrDict(): FishDict {
        val group = this.groupData[this.currGroupId.value]
        return group!!.getCurrDict()
    }
}